/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.pg.misra.Actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
import pl.pg.misra.AST.AST;
import pl.pg.misra.Interfaces.IRuleChecker;
import pl.pg.misra.Interfaces.ISourceParser;
import pl.pg.misra.Helpers.RuleChecker;
import pl.pg.misra.Helpers.SourceParser;

@ActionID(category = "Profile",
id = "pl.pg.misra.AST")
@ActionRegistration(displayName = "#CTL_AST")
@ActionReferences({
    @ActionReference(path = "Menu/Tools", position = 0, separatorAfter = 50)
})
@Messages("CTL_AST=MISRA-C Compliance")
public final class ASTAnalizer extends AbstractAnalizer implements ActionListener {
    final JFileChooser fc = new JFileChooser();
    final JFrame fileChooserFrame = new JFrame();
    ISourceParser parser = new SourceParser();
    IRuleChecker ruleChecker = new RuleChecker();
    
    @Override
    public void actionPerformed(ActionEvent e) {

        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);       
        
        if (fc.showOpenDialog(fileChooserFrame) == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            List<File> astFiles = parser.buildASTFiles(file);
            
            List<AST> result = parser.parseToAST(astFiles);
            
            List<String> ruleResult = ruleChecker.checkRules(result);
            
            updateResultTable(ruleResult);
            
        }
        else {
            System.out.println("No Selection ");
        }
    }
}
